package com.movitech.mobile.cz.base.purchase.controller;


import java.util.List;

import com.movitech.mobile.cz.base.purchase.entity.ext.ExtPContactPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.purchase.bizvo.PContactPlanVo;
import com.movitech.mobile.cz.base.purchase.entity.PContactPlan;
import com.movitech.mobile.cz.base.purchase.service.PContactPlanService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
/**
 * <p>
 * PContactPlan 前端控制器
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-08
 */
@RestController
@RequestMapping("/v1/pContactPlan")
@Api(value = "合约规划基础操作接口", description = "合约规划基础操作接口")
public class PContactPlanController {
    @Autowired
    private PContactPlanService pContactPlanService;

    @GetMapping("/allList")
    @ApiOperation(
            value = "获取所有合约规划接口",
            notes = "获取所有合约规划接口",
            response = PContactPlan.class
    )
    public RestReturnResult list() {
        RestReturnResult restResult = RestReturnResult.success();
        Page<PContactPlan> page = pContactPlanService.selectPage(new Page<PContactPlan>());
        restResult.setData(page);
        return restResult;
    }

    @PostMapping("/searchList/{pageNumber}")
    @ApiOperation(
            value = "合约规划列表查询接口",
            notes = "合约规划列表查询接口",
            response = PContactPlan.class
    )
    public RestReturnResult searchList(@RequestBody PContactPlan pContactPlan,
                @ApiParam(value = "分页页数")@PathVariable Integer pageNumber) {
        RestReturnResult restResult = RestReturnResult.success();
        Page<PContactPlan> page = new Page<>();
        page.setCurrent(pageNumber);
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.setEntity(pContactPlan);
        page = pContactPlanService.selectPage(page,wrapper);
        restResult.setData(page);
        return restResult;
    }

    @GetMapping("/getPContactPlan/{id}")
    @ApiOperation(
            value = "合约规划对象ID获取接口",
            notes = "合约规划对对象ID获取接口",
            response = PContactPlan.class
    )
    public RestReturnResult addPContactPlan(@PathVariable String id) {
        RestReturnResult restResult = RestReturnResult.success();
        PContactPlan pContactPlan = pContactPlanService.selectById(id);
        restResult.setData(pContactPlan);
        return restResult;
    }

    @GetMapping("/getByDetailId/{id}")
    @ApiOperation(
            value = "合约规划对象ID获取接口",
            notes = "合约规划对对象ID获取接口",
            response = PContactPlan.class
    )
    public RestReturnResult getPContactPlan(@PathVariable String id) {
        RestReturnResult restResult = RestReturnResult.success();
        List<ExtPContactPlan> pContactPlan = pContactPlanService.selectByDetailId(id);
        restResult.setData(pContactPlan);
        return restResult;
    }

    @PostMapping("/add")
    @ApiOperation(
            value = "合约规划添加接口",
            notes = "合约规划添加接口",
            response = PContactPlan.class
    )
    public RestReturnResult add(@RequestBody PContactPlanVo pContactPlan) {
        RestReturnResult restResult = RestReturnResult.success();
        pContactPlanService.saveOrUpdatePContactPlan(pContactPlan);
        restResult.setData(pContactPlan);
        return restResult;
    }

    @PutMapping("/update")
    @ApiOperation(
            value = "合约规划更新接口",
            notes = "合约规划更新接口",
            response = PContactPlan.class
    )
    public RestReturnResult update(@RequestBody PContactPlan pContactPlan) {
        RestReturnResult restResult = RestReturnResult.success();
        pContactPlanService.updateById(pContactPlan);
        restResult.setData(pContactPlan);
        return restResult;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(
            value = "合约规划通过ID删除接口",
            notes = "合约规划通过ID删除接口",
            response = PContactPlan.class
    )
    public RestReturnResult update(@PathVariable String id) {
        RestReturnResult restResult = RestReturnResult.success();
        pContactPlanService.deleteById(id);
        return restResult;
    }


    @PostMapping("/getProjectPhasesList")
    @ApiOperation(value = "项目分期列表查询接口", notes = "项目分期列表查询接口")
    public RestReturnResult getProjectPhasesList(){
    	RestReturnResult restResult = RestReturnResult.error();

    	try{
    		restResult = pContactPlanService.getProjectPhasesList();
    	}catch(Exception e){
    		e.printStackTrace();
    		restResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
    	}

    	return restResult;
    }

    /***
	 * 同步成本系统合约规划
	 * @param lstPContactPlanVo
	 * @return
	 */
    @ApiOperation(
    		 value = "同步成本系统合约规划接口",
             notes = "同步成本系统合约规划接口",
             response = RestReturnResult.class
    )
    @RequestMapping(value = "/synContactPlanFromCostSystem", method = RequestMethod.POST)
    @ResponseBody
	public RestReturnResult doSynContactPlanFromCostSystem(@RequestBody List<PContactPlanVo> lstPContactPlanVo) {
		RestReturnResult restResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		try{
			restResult = pContactPlanService.doSynContactPlanFromCostSystem(lstPContactPlanVo);
		}catch(Exception e){
    		e.printStackTrace();
			restResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		}
		return restResult;
	}
}

